import { login } from '@/api/user'
import storage from '@/utils/storage'
import router from '@/router'
const state = {
  token: storage.getItem('token')
}

const mutations = {
  SET_TOKEN(state, token) {
    state.token = token
  }
}

const actions = {
  login({ commit }, loginForm) {
    return new Promise((resolve, reject) => {
      login(loginForm)
        .then((res) => {
          storage.setItem('token', res.data.token)
          commit('SET_TOKEN', res.data.token)
          // router.push('/')
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  logout({ commit }) {
    commit('SET_TOKEN', '')
    storage.clearAll()
    router.push('/login')
  }
}

const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
